from django.shortcuts import render
from .models import OrgInfo, TeacherInfo, CityInfo
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from operations.models import UserLove
from django.db.models import Q


# Create your views here.
def org_list(request):
    all_orgs = OrgInfo.objects.all()
    all_cities = CityInfo.objects.all()
    sort_orgs = all_orgs.order_by('-love_num')[:3]

    # 全局搜索功能过滤
    keyword = request.GET.get('keyword', '')
    if keyword:
        all_orgs = all_orgs.filter(
            Q(name__icontains=keyword) |
            Q(desc__icontains=keyword) |
            Q(detail__icontains=keyword))

    # 按照机构类别进行过滤
    cate = request.GET.get('cate', '')
    if cate:
        all_orgs = all_orgs.filter(category=cate)

    # 按照所在地区进行过滤筛选
    cityid = request.GET.get('cityid', '')
    if cityid:
        all_orgs = all_orgs.filter(cityInfo_id=cityid)

    # 排序
    sort = request.GET.get('sort', '')
    if sort:
        all_orgs = all_orgs.order_by('-' + sort)

    # 分页
    page_num = request.GET.get('pagenum', '')
    pa = Paginator(all_orgs, 3)
    try:
        pages = pa.page(page_num)
    except PageNotAnInteger:
        pages = pa.page(1)
    except EmptyPage:
        pages = pa.page(pa.num_pages)

    return render(request, 'orgs/org-list.html', {
        'all_orgs': all_orgs,
        'all_cities': all_cities,
        'sort_orgs': sort_orgs,
        'pages': pages,
        'cate': cate,
        'cityid': cityid,
        'sort': sort,
        'keyword': keyword
    })


def org_detail(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=org_id)[0]

        org.click_num += 1
        org.save()
        # 打上是否收藏的标记,给前端做展示
        love_flag = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_status=True)
            if love:
                love_flag = True
        return render(request, 'orgs/org-detail-homepage.html', {
            'org': org,
            'detail_type': 'home',
            'love_flag': love_flag
        })


def org_detail_course(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=org_id)[0]
        all_course = org.courseinfo_set.all()

        # 打上是否收藏的标记,给前端做展示
        love_flag = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_status=True)
            if love:
                love_flag = True

        # 分页
        page_num = request.GET.get('pagenum', '')
        pa = Paginator(all_course, 3)
        try:
            pages = pa.page(page_num)
        except PageNotAnInteger:
            pages = pa.page(1)
        except EmptyPage:
            pages = pa.page(pa.num_pages)

        return render(request, 'orgs/org-detail-course.html', {
            'org': org,
            'pages': pages,
            'detail_type': 'course',
            'love_flag': love_flag
        })


def org_detail_desc(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=org_id)[0]

        # 打上是否收藏的标记,给前端做展示
        love_flag = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_status=True)
            if love:
                love_flag = True

        return render(request, 'orgs/org-detail-desc.html', {
            'org': org,
            'detail_type': 'desc',
            'love_flag': love_flag
        })


def org_detail_teacher(request, org_id):
    if org_id:
        org = OrgInfo.objects.filter(id=org_id)[0]

        # 打上是否收藏的标记,给前端做展示
        love_flag = False
        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_man=request.user, love_id=int(org_id), love_status=True)
            if love:
                love_flag = True

        return render(request, 'orgs/org-detail-teachers.html', {
            'org': org,
            'detail_type': 'teacher',
            'love_flag': love_flag
        })


def teacher_list(request):
    all_teachers = TeacherInfo.objects.all()
    sort_teachers = all_teachers.order_by('-love_num')[:2]

    # 全局搜索功能过滤
    keyword = request.GET.get('keyword', '')
    if keyword:
        all_teachers = all_teachers.filter(name__icontains=keyword)

    # 排序
    sort = request.GET.get('sort', '')
    if sort:
        all_teachers = all_teachers.order_by('-' + sort)

    # 分页
    page_num = request.GET.get('pagenum', '')
    pa = Paginator(all_teachers, 3)
    try:
        pages = pa.page(page_num)
    except PageNotAnInteger:
        pages = pa.page(1)
    except EmptyPage:
        pages = pa.page(pa.num_pages)

    return render(request, 'orgs/teachers-list.html', {
        'all_teachers': all_teachers,
        'sort_teachers': sort_teachers,
        'pages': pages,
        'sort': sort,
        'keyword': keyword,
    })


def teacher_detail(request, teacher_id):
    if teacher_id:
        teacher = TeacherInfo.objects.filter(id=teacher_id)[0]
        teacher.click_num += 1
        teacher.save()

        sort_teacher = TeacherInfo.objects.all().order_by('-love_num')[:2]

        # 用户是否收藏标记
        love_teacher_flag = False
        love_org_flag = False

        if request.user.is_authenticated:
            love = UserLove.objects.filter(love_id=teacher_id, love_type=3, love_status=True, love_man=request.user)
            if love:
                love_teacher_flag = True

            love = UserLove.objects.filter(love_id=teacher.work_company.id, love_type=1, love_status=True,
                                           love_man=request.user)
            if love:
                love_org_flag = True

        return render(request, 'orgs/teacher-detail.html', {
            'teacher': teacher,
            'sort_teacher': sort_teacher,
            'love_teacher_flag': love_teacher_flag,
            'love_org_flag': love_org_flag,
        })
